package me.lubinn.Vicincantatio;

//import com.platymuus.bukkit.permissions.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.util.*;
import java.util.logging.Logger;

import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityTNTPrimed;
import net.minecraft.server.MovingObjectPosition;
import net.minecraft.server.WorldServer;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.entity.*;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.util.Vector;
import org.bukkit.util.config.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.Map;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.event.entity.EntityTargetEvent;
import net.minecraft.server.EntityFireball;
import net.minecraft.server.Vec3D;
import org.bukkit.craftbukkit.entity.CraftArrow;
import org.bukkit.craftbukkit.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Explosive;
import org.bukkit.entity.Projectile;
import org.bukkit.event.entity.*;
import org.bukkit.plugin.PluginManager;

import ru.tehkode.permissions.PermissionManager;
import ru.tehkode.permissions.bukkit.PermissionsEx;

import me.lubinn.Vicincantatio.Spells.*;


 // Referenced classes of package me.orion.Vicincantatio:
//            Vicincantatio, VicincantatioPlayerListener

public class VicincantatioSpell
{
	
    private int size;
    public int n;
    public boolean book_augment;
    public Configuration config;
    public String components[];
    public PlayerChatEvent ev;
    public static VicincantatioPlayerListener plugin;
    public static Random rand = new Random();
    
//    private String _LowSecurityComponents[] =
//    	{ "vitrea", "ignis", "aquae", "glacies", "aranea", "folia", "aeris", "cactus",
//    	  "lapis", "obsidianus", "ferrum", "aurum", "adamas", "arena", "fimus" };
//    private String _HighSecurityComponents[] =
//    	{ "vitrea", "aquae", "glacies", "folia", "lapis", "ferrum", "aurum", "adamas",
//    	  "fimus" };
    
    private String listOfSpells[] =
    	{ "accendit", "annulus", "arbore", "bulla", "chasma tempestas", "concalesco", "cubus", "demis", "discus", "domus",
    		"evanescere", "extinguere", "frigidus", "fulmen", "globus ignis", "gradi", "hadoken", "ignis perpetua",
    		"implete aquas", "libyes", "lucem", "lux", "murus", "mutatio", "noctem", "pluvia", "praemium", "remedium",
    		"respiro", "sagitto", "sagitto pluvia", "salio", "sereno", "sphaera", "tempestas", "tholus", "turris",
    		"uanescere", "urgeo", "vello", "vorto",
    		"chunkus erasus", "chunkus renero", };
    
	public static final Map<String, Material> listOfMaterials = new HashMap<String, Material>();
	static
	{
		listOfMaterials.put("fimus", Material.DIRT);
		listOfMaterials.put("aquae", Material.STATIONARY_WATER);
		//listOfMaterials.put("aquae", Material.WATER);
		listOfMaterials.put("aeris", Material.AIR);
		listOfMaterials.put("vitrea", Material.GLASS);
		listOfMaterials.put("glacies", Material.ICE);
		listOfMaterials.put("aranea", Material.WEB);
		listOfMaterials.put("folia", Material.LEAVES);
		listOfMaterials.put("ignis", Material.FIRE);
		listOfMaterials.put("silice", Material.LAVA);
		//listOfMaterials.put("silice", Material.STATIONARY_LAVA);
		listOfMaterials.put("lapis", Material.STONE);
		listOfMaterials.put("obsidianus", Material.OBSIDIAN);
		listOfMaterials.put("ferrum", Material.IRON_BLOCK);
		listOfMaterials.put("aurum", Material.GOLD_BLOCK);
		listOfMaterials.put("adamas", Material.DIAMOND_BLOCK);
		listOfMaterials.put("arena", Material.SAND);
		listOfMaterials.put("herba", Material.GRASS);
		
		listOfMaterials.put("explodium", Material.TNT);
	}

    Thread runner;

    public VicincantatioSpell(VicincantatioPlayerListener instance)
    {
        size = 1;
        n = 0;
        book_augment = false;
        config = null;
        plugin = instance;
    }
    
    private boolean isIn(String list[], String str) //returns true if str is in list
    {
    	for (int i = 0; i < list.length; i++)
    		if (list[i].equalsIgnoreCase(str))
    			return true;
    	return false;
    }

    public void CastSpell(String components[], PlayerChatEvent ev)
    {
        config = Vicincantatio.config;
        this.components = components;
        this.ev = ev;
        n = 0;
        size = 2;
        book_augment = false;
        
        String spellName = "";
        Spell toCast = null;
        Material material = null; //TODO: remember lastMaterial
        Map<String, Boolean> augments = new HashMap<String, Boolean>();
        
        //ignore case on everything
        for (int i = 0; i < components.length; i++)
        	components[i] = components[i].toLowerCase();
        
        for (int i = 0; i < components.length; i++)
        {
        	//try and get the spell name
        	if (spellName.equals("") || spellName.equals("bulla"))
        	{
        		String foundSpell = "";
        		
        		if (Spell.GetSpell(components[i]) != null)
        			foundSpell = components[i];
	        	if (i < components.length-1)
	        	{
	    			String twoWords = components[i]+" "+components[i+1];
	        		if (Spell.GetSpell(twoWords) != null)
	        		{
	        			foundSpell = twoWords;
	        			i++; //skip next word; it's included in the spell name
	        		}
	        	}

	        	if (!foundSpell.equals(""))
	        	{
	        		if (spellName.equals("bulla"))
	        			augments.put("bubble", true);
	        		spellName = foundSpell;
	        		toCast = Spell.GetSpell(spellName);
	        		Vicincantatio.log.info("Spell name: "+spellName);
	        		continue;
	        	}
        	}
        	
        	//try and get size-changing words
        	if (size == 2)
        	{
        		//If PermissionsEx detected, check it then do stuff with the sizes
            	if(Bukkit.getServer().getPluginManager().isPluginEnabled("PermissionsEx"))
            	{
	            	PermissionManager permissions = PermissionsEx.getPermissionManager();
	            	
			        if (components[i].equalsIgnoreCase("ultima") && Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf("world_maiming"))).append("_enabled").toString(), true))
			        {
		                if ((permissions.has(ev.getPlayer(), "ultima" + ".allow")))
				        	size = 9;
		                else
		                {
		                	Vicincantatio.log.info("Ultima size not allowed!");
		                	toCast = null;
		                }
			        }  
			        else if (components[i].equalsIgnoreCase("supera") && Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf("world_maiming"))).append("_enabled").toString(), true))
			        {
			        	if ((permissions.has(ev.getPlayer(), "supera" + ".allow")))
			        		size = 6;
			        	else
			        	{
			        		Vicincantatio.log.info("Supera size not allowed!");
			        		toCast = null;
			        	}
			        }
			        else if (components[i].equalsIgnoreCase("magna"))
			            size = 4;
			        else if (components[i].equalsIgnoreCase("parva"))
			            size = 1;
			        else if (components[i].equalsIgnoreCase("infera"))
			        	size = 0;
			        
			        if (size != 2)
			        {
	                    //Vicincantatio.log.info("Size: "+String.valueOf(size));
			        	continue;
			        }
            	}
            	
            	//If no permissions plugin
            	else
            	{
            		if (components[i].equalsIgnoreCase("ultima") && Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf("world_maiming"))).append("_enabled").toString(), true))
            			size = 9;
			        else if (components[i].equalsIgnoreCase("supera") && Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf("world_maiming"))).append("_enabled").toString(), true))
			        	size = 6;		        
			        else if (components[i].equalsIgnoreCase("magna"))
			            size = 4;
			        else if (components[i].equalsIgnoreCase("parva"))
			            size = 1;
			        else if (components[i].equalsIgnoreCase("infera"))
			        	size = 0;
			        
			        if (size != 2)
			        {
	                    //Vicincantatio.log.info("Size: "+String.valueOf(size));
			        	continue;
			        }
            	}
            	
        	}
        	
        	//try and get the material
        	if (material == null)
        	{
        		//Permissions on material
        		if(Bukkit.getServer().getPluginManager().isPluginEnabled("PermissionsEx"))
            	{
	            	PermissionManager permissions = PermissionsEx.getPermissionManager();
	        		if (listOfMaterials.containsKey(components[i]))
	        		{
	        			if (permissions.has(ev.getPlayer(), components[i] + ".allow"))
	        			{
	        				material = listOfMaterials.get(components[i]);
	        				Vicincantatio.log.info("Material: "+components[i]);
	        				continue;
	        			}
	        			else
	        			{
	        				Vicincantatio.log.info("Material not allowed.");
	        				ev.getPlayer().sendMessage("Sorry, you don't have the permissions for that material");
	        			}
	        		}
            	}
        		//if there is no plugin
        		else
        		{
        			if (listOfMaterials.containsKey(components[i]))
	        		{
	        			material = listOfMaterials.get(components[i]);
	        			Vicincantatio.log.info("Material: "+components[i]);
	        			continue;
	        		}
        		}
        	}
        	
        	if (components[i].equalsIgnoreCase("augere") && i+1 < components.length)
        	{
	        	if (components[i+1].equalsIgnoreCase("vi"))
	        		augments.put("force", true);
	        	else if (components[i+1].equalsIgnoreCase("volo"))
	        		augments.put("flight", true);
	        	else if (components[i+1].equalsIgnoreCase("motor"))
	        		augments.put("motion", true);
	        	else if (components[i+1].equalsIgnoreCase("silentium"))
	        		augments.put("silence", true);
	        	
	        	//Nick here, doin' mah delay spell   lalalala
	        	else if (components[i+1].equalsIgnoreCase("mora"))
	        	{
	        		Delay.delay(spellName, toCast, ev, material, size, augments);
	        		
	        		//this is to NOT cast on delay
	        		toCast = null;
	        	}
	        	
	        		
        	}
        	if (components[i].equalsIgnoreCase("bulla"))
        		augments.put("bubble", true);
        	
        	//Moar delayness weeeee
        	if (components[i].equalsIgnoreCase("libero"))
        		Delay.release(ev);
        }
        
        if (!spellName.equals(""))
        {
        	boolean canCast = true;
        	
        	//check for book augment/requirement
            if(Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf(spellName))).append("_consumes_book").toString(), false))
            {
                if (!PlayerSpendReagent(ev.getPlayer(), Material.BOOK, 1))
                	canCast = false;
            }
            if(Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf(spellName))).append("_book_augment").toString(), false))
            {
                if (PlayerSpendReagent(ev.getPlayer(), Material.BOOK, 1))
                	augments.put("book", true);
            }

            if (toCast != null && canCast)
            {
            	//If PermissionsEx detected, check it then do stuff
            	if(Bukkit.getServer().getPluginManager().isPluginEnabled("PermissionsEx"))
            	{
	            	PermissionManager permissions = PermissionsEx.getPermissionManager();
	                if ((permissions.has(ev.getPlayer(), spellName + ".allow")))
	                	 toCast.CastSpell(ev, material, size, augments);
	                 
	                else
	                {
	                	 Vicincantatio.log.info("Spell not allowed by user.");
	                	 ev.getPlayer().sendMessage("Sorry, you do not have permission to do that.");
	                	
	                	 //Debug
	                	// Vicincantatio.log.info(spellName + ".allow");
	                }
            	}
            	
//            	//If BukkitPermissions detected, check it then do stuff
//            	if(Bukkit.getServer().getPluginManager().isPluginEnabled("BukkitPermissions"))
//            	{
//	            	PermissionManager permissions = BukkitBermissions.g
//	                if ((permissions.has(ev.getPlayer(), spellName + ".allow")))
//	                	 toCast.CastSpell(ev, material, size, augments);
//	                 
//	                else
//	                {
//	                	 Vicincantatio.log.info("Spell not allowed by user.");
//	                	 ev.getPlayer().sendMessage("Sorry, you do not have permission to do that.");
//	                	
//	                	 //Debug
//	                	// Vicincantatio.log.info(spellName + ".allow");
//	                }
//            	}
            	
            	
            	//Permissions not detected, cast anyway
            	else
            		toCast.CastSpell(ev, material, size, augments);
                 
            }

            if (augments.containsKey("silence"))
            	ev.setCancelled(true);
        }
    }
    
    boolean CheckPermissions(String spell, String components[])
    {	
        if(Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf(spell))).append("_consumes_book").toString(), false))
        {
            return PlayerSpendReagent(ev.getPlayer(), Material.BOOK, 1);
        }
        if(Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf(spell))).append("_book_augment").toString(), false))
        {
            book_augment = PlayerSpendReagent(ev.getPlayer(), Material.BOOK, 1);
        }
    	return true;
    	
    	
    }
    
    //THIS DOES NOTHING!! RAWR
    boolean Hold_place(String spell, String components[]) //FIXME: re-enable permissions!! 
    {
        if(!Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf(spell))).append("_enabled").toString(), true))
        {
            return false;
        }
        if(Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf(spell))).append("_enabled").toString(), true) && spell == "celeritas")
        {
            return true;
        }
        if(Vicincantatio.config.getBoolean("all_spells_require_book", false) && ev.getPlayer().getItemInHand().getType() != Material.BOOK)
        {
            return false;
        }
        if(Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf(spell))).append("_consumes_book").toString(), false))
        {
            return PlayerSpendReagent(ev.getPlayer(), Material.BOOK, 1);
        }
        
        
        
        
        
        if(!Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf(spell))).append("_magna_parva").toString(), false))
        {
            size = 2;
        }
        if(Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf(spell))).append("_book_augment").toString(), false))
        {
            book_augment = PlayerSpendReagent(ev.getPlayer(), Material.BOOK, 1);
        }
        return true;
    }

    private boolean IsForceful(String components[], PlayerChatEvent ev2)
    {
    	int l = components.length;
    	if (l < 2) return false;
        return components[l - 2].equalsIgnoreCase("augere") && components[l - 1].equalsIgnoreCase("vi");
    }

    private void IsSilent(String components[], PlayerChatEvent ev2, int i)
    {
    	int l = components.length;
        if (l < 2) return;
        if(components[l - 2].equalsIgnoreCase("augere") && components[l - 1].equalsIgnoreCase("silentium"))
            ev2.setCancelled(true);
    }
    
    private boolean IsMotion(String components[], PlayerChatEvent ev)
    {
    	int l = components.length;
    	if (l < 2) return false;
        return components[l - 2].equalsIgnoreCase("augere") && components[l - 1].equalsIgnoreCase("motor");
    }
    
    private boolean IsFlying(String components[], PlayerChatEvent ev)
    {
    	int l = components.length;
    	if (l < 2) return false;
        return components[l - 2].equalsIgnoreCase("augere") && components[l - 1].equalsIgnoreCase("volo");
    }

    public boolean PlayerSpendReagent(Player player, Material reagent, int count)
    {
        PlayerInventory inventory = player.getInventory();
        int slot = inventory.getHeldItemSlot();
        ItemStack item = player.getItemInHand();
        int amount = item.getAmount();
        Material typeid = item.getType();
        if(typeid != reagent)
        {
            return false;
        }
        if(amount > count)
        {
            item.setAmount(amount - count);
            return true;
        }
        if(amount < count)
        {
            return false;
        }
        if(amount == count)
        {
            inventory.clear(slot);
            return true;
        } else
        {
            return false;
        }
    }

    private void ImpleteAquas(String components[], PlayerChatEvent ev)
    {
        int radius = 6 + 4*size;
        Block block = ev.getPlayer().getTargetBlock(null, 500);
        Location origin = block.getLocation();
        int xorg = (int)origin.getX();
        int yorg = (int)origin.getY();
        int zorg = (int)origin.getZ();
        if(Math.abs(yorg - 63) < radius)
        {
            int y = 63;
            for(int x = xorg - radius - 1; x <= xorg + radius + 1; x++)
            {
                for(int z = zorg - radius - 1; z <= zorg + radius + 1; z++)
                {
                    int dx = x - xorg;
                    int dz = z - zorg;
                    if((int)Math.sqrt(dx * dx + dz * dz) <= radius)
                    {
                        Block targetblock = ev.getPlayer().getWorld().getBlockAt(x, y, z);
                        Material type = targetblock.getType();
                        if(type == Material.AIR || type == Material.WATER || type == Material.STATIONARY_WATER)
                        {
                            targetblock.setType(Material.STATIONARY_WATER);
                            //targetblock.getWorld().playEffect(targetblock.getLocation(), Effect.SMOKE, 1);
                        }
                    }
                }

            }

        }
    }


    private Material GetMaterial(String latinname)
    {
        if(latinname.equalsIgnoreCase("aquae"))
            return Material.STATIONARY_WATER;
        if(latinname.equalsIgnoreCase("aeris"))
            return Material.AIR;
        if(latinname.equalsIgnoreCase("vitrea"))
            return Material.GLASS;
        if(latinname.equalsIgnoreCase("glacies"))
            return Material.ICE;
        if(latinname.equalsIgnoreCase("aranea"))
            return Material.WEB;
        if(latinname.equalsIgnoreCase("folia"))
            return Material.LEAVES;
        if(latinname.equalsIgnoreCase("ignis"))
            return Material.FIRE;
        if(latinname.equalsIgnoreCase("silice"))
            return Material.LAVA;
        if(latinname.equalsIgnoreCase("cactus"))
        	return Material.CACTUS;
        if(latinname.equalsIgnoreCase("lapis"))
        	return Material.STONE;
        if(latinname.equalsIgnoreCase("obsidianus"))
        	return Material.OBSIDIAN;
        if(latinname.equalsIgnoreCase("ferrum"))
        	return Material.IRON_BLOCK;
        if(latinname.equalsIgnoreCase("aurum"))
        	return Material.GOLD_BLOCK;
        if(latinname.equalsIgnoreCase("adamas"))
        	return Material.DIAMOND_BLOCK;
        if(latinname.equalsIgnoreCase("arena"))
        	return Material.SAND;
        if(latinname.equalsIgnoreCase("fimus"))
        	return Material.DIRT;
        if(latinname.equalsIgnoreCase("herba"))
        	return Material.GRASS;
        if(latinname.equalsIgnoreCase("nether"))
        	return Material.NETHERRACK;
        return null;
    }



    private void Murus(String components[], PlayerChatEvent ev)
    {
        //String spell = "murus";
        //if(Arrays.asList(_MurusAllowedComponents).contains(components[n + 1]) && Vicincantatio.config.getBoolean((new StringBuilder(String.valueOf(spell))).append("_").append(components[n + 1]).toString(), false))
        {
            boolean overwrite = IsForceful(components, ev);
            Material component = GetMaterial(components[n + 1]);
            int length = 5 + 15*size/2;
            Vector vector = ev.getPlayer().getEyeLocation().getDirection().normalize();
            Location location;
            if (IsFlying(components, ev))
            	location = ev.getPlayer().getTargetBlock(null, 10 + 5*size).getLocation();
            else
            	location = ev.getPlayer().getTargetBlock(null, 500).getLocation();
            int xorg = location.getBlockX();
            int yorg = location.getBlockY();
            int zorg = location.getBlockZ();
            double vx = vector.getX();
            double vz = vector.getZ();
            double ox = -vz;
            double oz = vx;
            for(int i = -length / 2; i <= length / 2 + 1; i++)
            {
                location.setX((double)xorg + (double)i * ox);
                location.setZ((double)zorg + (double)i * oz);
                for(int y = Math.max(yorg, 1); y <= Math.min(yorg + length/2, 120); y++)
                {
                    location.setY(y);
                    Block block = location.getBlock();
                    Material type = block.getType();
                    if(overwrite && type != Material.BEDROCK)
                    {
                        block.setType(component);
                        //block.getWorld().playEffect(block.getLocation(), Effect.SMOKE, 1);
                    } else
                    if(type == Material.AIR)
                    {
                        block.setType(component);
                        //block.getWorld().playEffect(block.getLocation(), Effect.SMOKE, 1);
                    }
                }

            }
            if (IsMotion(components, ev))
            	ev.getPlayer().teleport(new Vector(xorg, yorg+length/2+1, zorg).toLocation(ev.getPlayer().getWorld()));
        }
    }

    
    
    
      private void Scalae(String components[], PlayerChatEvent ev)
      {
    	  boolean ow = IsForceful(components, ev);
    	  Material _cp = null;// GetComponent(components[n + 1]);
    	  Location location;
    	  location = ev.getPlayer().getTargetBlock(null, 500).getLocation();
    	  int x = location.getBlockX();
    	  int y = location.getBlockY();
    	  int z = location.getBlockZ();
    	  for (int scale = 0; scale<=size; scale++)
    	  {
/*    		  //loop to clear 3x3 column above or below
    		  for(int yc = y-1; yc <= y+8*scale+1; yc++)
    	      {
    			  for(int xc = x - 1; xc <= x + 1; xc++)
    		      {
    		            for(int zc = z - 1; zc < z + 1; zc++)
    		            {
    		                {
    		                    Block block = ev.getPlayer().getWorld().getBlockAt(xc, yc, zc);
    		                    Material type = block.getType();
    		                    if(type != Material.BEDROCK)
    		                    {
    		                        block.setType(Material.AIR);
    		                        //block.getWorld().playEffect(block.getLocation(), Effect.SMOKE, 1);
    		                    }
    		                }
    		            }
    		        }
    	        }
*/    		  //start single-ascension loop.  Assume 1,2,3,6,9,8,7,4, always 5.
    		  write_block(ow, x-1, y, z-1, _cp);
    		  write_block(ow, x-1, y+1, z, _cp);
    		  write_block(ow, x-1, y+2, z+1, _cp);
           	  write_block(ow, x, y+3, z+1, _cp);
           	  write_block(ow, x+1, y+4, z+1, _cp);
           	  write_block(ow, x+1, y+5, z, _cp);
           	  write_block(ow, x+1, y+6, z-1, _cp);
           	  write_block(ow, x, y+7, z-1, _cp);
           	  for (int h=0; h<8; ++h)
           		  write_block(ow, x, y++, z, _cp);
           	  
    	  }
    	  if (IsMotion(components, ev))
    		  ev.getPlayer().teleport(location.toVector().add(new Vector(0, (size+1)*8, 0)).toLocation(ev.getPlayer().getWorld()));
    	  
	  }
      private void write_block(boolean overwrite, int x, int y, int z, Material _cmpnt)
      {
    	  Block _bk = ev.getPlayer().getWorld().getBlockAt(x, y, z);
    	  Material type = _bk.getType();
    	  if(type == Material.AIR || (overwrite && type != Material.BEDROCK))
    	  {
    		  _bk.setType(_cmpnt);
    		  _bk.getWorld().playEffect(_bk.getLocation(), Effect.SMOKE, 1);
    	  }
      }

      
//      private void reflectShit(String components[], PlayerChatEvent ev)
//      {
//    	  CraftWorld world = (CraftWorld)ev.getPlayer().getWorld();
//    	  
//    	  //make wall perpendicular to look direction
//    	  boolean overwrite = IsForceful(components, ev);
//          Material component = GetComponent(components[n + 1]);
//          int length = 5 + 15*size/2;
//          Vector vector = ev.getPlayer().getEyeLocation().getDirection().normalize();
//          Location location;
//          location = ev.getPlayer().getTargetBlock(null, 500).getLocation();
//          int xorg = location.getBlockX();
//          int yorg = location.getBlockY();
//          int zorg = location.getBlockZ();
//          double vx = vector.getX();
//          double vz = vector.getZ();
//          double ox = -vz;
//          double oz = vx;
//          for(int i = -length / 2; i <= length / 2 + 1; i++)
//          {
//              location.setX((double)xorg + (double)i * ox);
//              location.setZ((double)zorg + (double)i * oz);
//              for(int y = Math.max(yorg, 1); y <= Math.min(yorg + length/2, 120); y++)
//              {
//                  location.setY(y);
//                  Block block = location.getBlock();
//                  Material type = block.getType();
//                  if(overwrite && type != Material.BEDROCK)
//                  {
//                	  //Look like portal			need to make a special material
//                      block.setType(Material.PORTAL);
//                      //block.getWorld().playEffect(block.getLocation(), Effect.SMOKE, 1);
//                  } else
//                  if(type == Material.AIR)
//                  {
//                	  //look like portal 			need to make a special material
//                      block.setType(Material.PORTAL);
//                      //block.getWorld().playEffect(block.getLocation(), Effect.SMOKE, 1);
//                  }
//              }
//
//          }
//          
//      }
      
//      
      
      //TODO: make it look for this plugin, then based on that result enable/disable this
//      private void Smashed(String components[], PlayerChatEvent ev)
//      {
//          Location location = ev.getPlayer().getTargetBlock(null, 500).getLocation();
//          World world = ev.getPlayer().getWorld();
//          List<Entity> entities = world.getEntities();
//          for(Iterator<Entity> iterator = entities.iterator(); iterator.hasNext();)
//          {
//              Entity entity = iterator.next();
//              Location oloc = entity.getLocation();
//              if(location.distance(oloc) <= 5D && ((entity instanceof Player)))
//              {
//                  MuddersMilk milk = null;
//                  milk.moreDrunk((Player)entity);
//                  milk.moreDrunk((Player)entity);
//                  milk.moreDrunk((Player)entity);
//                  milk.moreDrunk((Player)entity);
//                  milk.moreDrunk((Player)entity);
//                  milk.moreDrunk((Player)entity);
//              }
//          }
//
//      }
      
//      private Material reflectoid()
//      {
//    	  return 
//      }
      // VERY USEFUL SHIT: 
      // Durations framework
      
      
      //K, so, in theory, all you need to do is throw down a while loop over this boolean until its false
      //FUUUUUUUUUUUUUUUCK
      // NEVERMIND
      //BAD ME
      // NOTE: Will an action listener fuck with everything?
//      new Timer(delay, taskPerformer).start();
//      
//      int delay = 1000; //milliseconds
//      ActionListener taskPerformer = new ActionListener() 
//      {
//          public void actionPerformed(ActionEvent evt) 
//          {
//              //...Perform a task...
//          }
//      };
//      
      
       
}
